<?php

namespace app\controllers;

use Yii;
use yii\base\Exception;
use yii\filters\AccessControl;
use yii\web\AppController;
use yii\web\Utils;
use yii\helpers\Url;
use yii\helpers\BaseArrayHelper;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use himiklab\jqgrid\actions\JqGridActiveAction;
use yii\web\NicImgUploadResponse;
use app\models\WA_GROUP;

class WaAnnouncementController extends AppController
{
	public $title = 'WA003 แก้ไขข้อความประชาสัมพันธ์';
	public $enableCsrfValidation = false;
	public $imagePath = 'images/waannouncement';
	
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],

        ];
    }
    public function actionIndex()
    {
    	$request = Yii::$app->request;
    	
    	$rtparams = $request->post('rtparams'); $rtparams = (empty($rtparams)?$request->get('rtparams'):$rtparams);
    	
    	$this->addBreadCrumb($this->title);
    	
    	$params['rtparams']=$rtparams;
        return $this->render('index', $params);
    }
    
    public function actionUploadimg(){
    	$request = Yii::$app->request;
    	 
    	$directory = $this->imagePath;
    	
    	$fileName = $_FILES["image"]["name"];
    	
    	$splitFileName = explode(".", $fileName);
    	$extensionFile = ".".$splitFileName[count($splitFileName)-1];
    	$fileName = date('Ymd-His').$extensionFile;
    	
    	Yii::trace('directory = '.$directory.'  fileName = '.$fileName);
    	
    	$response = new NicImgUploadResponse();
    	$type='';
    	$width='';
    	$height='';
    	$size='';
    	$img='';
    	
    	if ( Utils::checkDirectory($directory) ) {
    	
    		if ( move_uploaded_file($_FILES["image"]["tmp_name"]	// temp_file
    				, $directory."/".$fileName) ) {	// path file
    				
//     			$constTaskImage = CONST_TASK_IMAGE::findOne($id);
//     			$constTaskImage->IMAGE_PATH = $fileName;
//     			if($constTaskImage->save()){
//     				$response->success();
//     			}else{
//     				$response->error(); //3
//     			}

    			list($width, $height, $typex, $attr) = getimagesize($directory."/".$fileName);

    			$type = $_FILES["image"]["type"];
    			$img = $directory."/".$fileName;
    		} else {
//     			$response->error(); //1
    		}
    			
    	} else {
//     		$response->error(); //2
    	}
    	
//     	$status['done'] = 1;
//     	$status['width'] = $size[0];
// //     	$rp = realpath($path);
//     	$status['url'] =  NICUPLOAD_URI ."/".$id;
    	
    	echo $response->getResponse($type, $width, $height, $size, $img);
    }
    
    public function actionSave(){
    	$request = Yii::$app->request;
    	$contents = $request->post('inputcontents');
    	
    	$myfile = fopen("../views/site/annoucement.php", "w") or die("Unable to open file!");
		fwrite($myfile, $contents);
		fclose($myfile);
		
		Yii::$app->getResponse()->redirect(['site']);
    }
}
